"use strice";

const Controller = require('../common/shop')

class orderController extends Controller {
  tableName = 'order'
  // 查询订单列表
  async orderList() {
    let siteId = await this.getSiteId()
    let site_id = siteId[0].site_id

    const {cur_page=1, each_page=10} = this.ctx.request.query
    let orderData = await this.consultPage({site_id}, null, {curPage: cur_page, eachPage: each_page})

    this.tableName = 'order_goods'
    let oGoodsPm = []
    orderData.list.forEach((orderItem) => {
      oGoodsPm.push(this.consult({order_id: orderItem.order_id}))
    });
    let goodsList = await Promise.all(oGoodsPm)

    orderData.list.forEach((orderItem, index) => {
      orderItem.goodsList = goodsList[index]
    })

    this.resData(0, '订单列表获取成功', orderData)
  }
  
  // 修改订单状态
  async orderStatus() {
    let siteId = await this.getSiteId()
    let result = await this.update({ site_id: siteId[0].site_id })
    this.resData(0, '订单状态修改成功', result)
  }

  // 获取用户订单详情
  async orderlist() {
    let member_id = this.ctx.request.query.id
    let result = await this.consult({ member_id })
    this.resData(0, '会员列表获取成功', result);
  }
}

module.exports = orderController